layui.extend({
    baseConfig : "/js/pages/baseConfig"
})
layui.use(['form','layer','laydate','table','laytpl','element','util','baseConfig'],function(){

    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        baseConfig = layui.baseConfig,
        table = layui.table;
    var util = layui.util;
    var tableIns = table.render({
        elem: '#list',
        url : '/systemRecord/list/',
        text: { none: "Aucun résultat"},
        cellMinWidth : 95,
        height : "full-10",
        id : "listTable",
        cols : [[
            {field: 'name', title: "备份名称", align:"center"},
            {field: 'backUpCount', title: "备份信息统计",align:"center"},
            {field: 'createTime', title: '备份时间', align:'center', templet : function( d){
                return baseConfig.formatDate( d.createTime);
            }},
            {title: 'État', templet:'#listBar',fixed:"right", align:"center"}
        ]],
        response : {
            statusCode: 200 //规定成功的状态码，默认：0
        },
        parseData : function( res){ //res 即为原始返回的数据
            return {
                "code": res.code, //解析接口状态
                "msg": res.msg, //解析提示文本
                "count": res.data.length, //解析数据长度
                "data": res.data //解析数据列表
            };
        }
    });

    //添加
    $(".backUp_btn").click( function(){
        layer.confirm('确定进行系统备份？将备份消费记录表、充值记录表、员工信息表、商品表四处的数据！', {icon: 3, title: '备份确认'}, function (confirmIndex) {
            layer.close( confirmIndex);
            var index = layui.layer.open({
                title : "备份",
                type : 2,
                area: [600 + 'px', 300 + 'px'],
                shade: 0.4,
                btnAlign: 'r',
                id: "ADD_backUp", //设定一个id，防止重复弹出
                content : "uploadCustomsClearanceForm.html",
                success : function(){
                    var body = layui.layer.getChildFrame( 'body', index);
                    setTimeout( function(){
                        layui.layer.tips('点击此处返回数据列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    },500);
                }
            });
        });
    });

    //添加
    $(".uplpad_backUp_btn").click( function(){
        var index = layui.layer.open({
            title : "上传备份文件",
            type : 2,
            area: [600 + 'px', 400 + 'px'],
            shade: 0.4,
            btnAlign: 'r',
            id: "UPLOAD_backUp", //设定一个id，防止重复弹出
            content : "uploadCustomsClearanceForm.html",
            success : function(){
                var body = layui.layer.getChildFrame( 'body', index);
                body.find( "#filePathDiv").attr( "display", "block");
                setTimeout( function(){
                    layui.layer.tips('点击此处返回数据列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500);
            }
        });
    });

    $(".systemRedo_btn").click( function(){
        layer.confirm('确定进行系统初始化？此操作将清除消费记录、充值记录、员工信息、商品表四处的数据！', {icon: 3, title: '备份确认'}, function (confirmIndex) {
            layer.close( confirmIndex);
            //弹出loading
            var loadingIndex = top.layer.msg('系统初始化中，请稍候', {icon: 16, time: false, shade: 0.8});
            //提交数据
            $.post("/systemRecord/redo",{}, function (res) {
                top.layer.close( loadingIndex);
                if (res.code == 200) {
                    setTimeout(function () {
                        top.layer.msg( res.msg);
                        tableIns.reload();
                    }, 500);
                } else if (res.code == 60000) {
                    top.layer.msg( res.msg);
                } else {
                    top.layer.msg( res.msg);
                }
            });
        });
    });

    function restore( data){
        layer.confirm('确定要重建系统？重建系统会删除当前系统所有的数据，再还原到备份的时间节点，请谨慎操作！！！', {icon: 3, title: '提示信息'}, function (index) {
            layer.close( index);
            //弹出loading
            var loadingIndex = top.layer.msg('系统数据还原中，请稍候',{icon: 16,time:false,shade:0.8});
            //提交数据
            $.post("/systemRecord/restore",{
                id : data.id
            },function( res){
                setTimeout( function() {
                    top.layer.close( loadingIndex);
                    if (res.code == 200) {
                        setTimeout(function () {
                            top.layer.msg( res.msg);
                        }, 500);
                    }else if (res.code == 80000) {
                        setTimeout(function () {
                            top.layer.msg( res.msg);
                        }, 500);
                    } else {
                        top.layer.msg( res.msg);
                    }
                },500);
            });
        });
    }

    //删除
    function del( ids){
        layer.confirm('确定删除选中的数据项？', {icon: 3, title: '提示信息'}, function (index) {
            $.post( "/systemRecord/del",{
                "ids" : ids
            },function( res){
                if( res.code == 200){
                    tableIns.reload();
                    layer.close( index);
                    top.layer.msg( res.msg);
                }else{
                    top.layer.close( index);
                    top.layer.msg( res.msg);
                }
            });
        });
    }

    //列表操作
    table.on('tool(list)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        if( layEvent === 'edit'){ //编辑
            restore( data);
        }else if( layEvent === 'del'){ //删除
            del( data.id);
        }else if( layEvent === 'downloadBackUp'){//下载备份文件
            $.post( "/systemRecord/download",{
                "id" : id
            },function( res){
                if( res.code == 200){
                    window.location.href = res.data;
                }else{
                    top.layer.msg( res.msg);
                }
            });
        }
    });
});
